#!/bin/sh
#
# batch -- create multiple containers in one script
#
# usage:
#
# E.x.:
# 1.
#    users="a b c d e f g" peruser=3 labs="linux-0.11-lab cs630-qemu-lab" \
#          host="myhost" cleanup=1 security=0 tools/deploy/batch
# 2.
#    prompt="" loop=2 interval=20 alive=20 cleanup=1 labs="linux-0.11-lab cs630-qemu-lab" tools/deploy/batch
#
# 3.
#    loop=0 alive=20 cleanup=1 labs="linux-0.11-lab cs630-qemu-lab" tools/deploy/batch
#
# 4.
#    loop=1 labs="linux-0.11-lab cs630-qemu-lab" alive=10 interval=10 alive_check=5 notify_wait=5 tools/deploy/batch
#
## Notes
#
# loop=0, one running
# loop=1, forever running
# loop=n (>1), n running

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR"/tools/docker/config $* >/dev/null

[ -z "$users" ] && users="ubuntu"
[ -z "$peruser" ] && peruser=1
[ -z "$labs" ] && labs=linux-0.11-lab
[ -z "$cleanup" ] && cleanup=0
[ -z "$security" ] && security=2
[ -z "$host" ] && host=$HOST
[ -z "$alive" ] && alive=""
[ -z "$interval" ] && interval=""
# loop =1 for always, 0 means 1 time, 2 means 2 times...
[ -z "$loop" ] && loop=1
[ -z "$prompt" ] && prompt='Hello, Cloud Lab!'
[ -z "$empty_prompt" ] && empty_prompt='当前没有任何 Lab 在线运行!'
[ -z "$cpus" ] && cpus="0-3"
[ -z "$notify_wait" ] && notify_wait=30
[ -z "$alive_check" ] && alive_check=30

LAB_SECURITY=$security
export prompt empty_prompt

. "${TOP_DIR}/tools/deploy/init"

echo "LOG: Start batch running @ `date`"

count=0

while :;
do
  count=$((count+1))
  echo "LOG: Start deploy $count"

  for lab in $labs
  do
    echo "LOG: Choose $lab"
    PULL=0 SUBMODULE=0 "${TOP_DIR}/tools/docker/choose" $lab

    if [ $cleanup -eq 1 ]; then
      echo "LOG: Clean up $lab"
      yes yes | "${TOP_DIR}/tools/deploy/clean" $lab

      echo "LOG: Update release page for $lab"
      lab_life=$alive "${TOP_DIR}/tools/deploy/release" all
      echo $lab > "$LAB_CURRENT"
    fi

    echo "LOG: Unlock $lab"
    "${TOP_DIR}/tools/docker/unlock" $lab

    echo $host > "${TOP_DIR}/.host_name"

    for user in $users
    do
      echo "LOG: Run $lab for $user"
      lab_alive=$alive lab_cpus=$cpus "${TOP_DIR}/tools/deploy/run" $lab $user $peruser

      echo "LOG: Release $lab"
      lab_life=$alive "${TOP_DIR}/tools/deploy/release" all
    done
  done

  if [ -n "$alive" ]; then
    alive_seconds=`time_to_seconds $alive`
    echo "LOG: Allow $lab running for `time_to_strings $alive`."
    for i in `seq 1 $alive_check $alive_seconds`
    do
      sleep $alive_check
      echo "LOG: Check alive $i of $alive_seconds"
    done

    export NOTIFY_WAIT=$notify_wait
    export NOTIFY_MSG="Lab will be removed"

    for lab in $labs
    do
      echo "LOG: Clean up $lab"
      (yes yes | "${TOP_DIR}/tools/deploy/clean" $lab) &
    done

    # Wait for every lab finish cleaning
    last_lab=`echo $labs | tr -s ' ' | tr ' ' '\n' | tail -1`
    lab_total=`echo $labs | tr -s ' ' | tr ' ' '\n' | wc -l`

    for i in `seq 1 $((NOTIFY_WAIT*2+lab_total))`
    do
      finish=0
      for lab in $labs
      do
        echo "LOG: Checking clean up status of $lab"
        ls -d "$LAB_OUTPUT/${lab}-*" >/dev/null 2>&1
        [ $? -eq 0 ] && echo "LOG: $lab still in cleaning" && break

        echo "LOG: $lab clean up done"
        [ $lab = $last_lab ] && finish=1 && break
      done
      [ $finish -eq 1 ] && break
      sleep $peruser
    done

    unset NOTIFY_WAIT
    unset NOTIFY_MSG

    echo "LOG: Empty release page" && get_lab_info
  fi

  echo "LOG: End deploy $count"
  [ $loop -eq 0 ] && break
  [ $loop -ne 1 -a $count -ge $loop ] && echo "LOG: Reached $loop times" && break

  if [ -n "$interval" ]; then
    interval_seconds=`time_to_seconds $interval`
    echo "LOG: sleep for ${interval} (${interval_seconds} seconds)."
    sleep $interval_seconds
  fi

done

echo "LOG: End batch running @ `date`"
